<?php
namespace App\Http\Helper;
use App\Model\CronModel;
use Illuminate\Support\Facades\Log;

/**
 * Created by PhpStorm.
 * User: hgzyc
 * Date: 18-4-21
 * Time: 下午2:17
 */
class CrontabFile {

    public static function write_file(){
        $file_name=date('m-d-H-i',time()).'-'.random_int(10000,99999);
        $dir_name=date('Ym');
        if( !file_exists(storage_path($dir_name))) {
            mkdir(storage_path($dir_name,0777));
        }
        $cron_text='#!/bin/bashsource/etc/profilesource$HOME/.bashrc'."\n";
        foreach (CronModel::all() as $v){
            $cron_text .=$v->time.' '.$v->shell."\n";
        }

        Log::info(public_path($dir_name.DIRECTORY_SEPARATOR.$file_name));
        file_put_contents(storage_path($dir_name.DIRECTORY_SEPARATOR.$file_name),$cron_text);
        system('crontab -u hgzyc '.storage_path($dir_name.DIRECTORY_SEPARATOR.$file_name));
    }




}